//#include <base.js>
//#include <openlayersExt.js>
//#include <event.js>


/**
* 
* Classe CWN2.Map - Inherited from Openlayers.Map
* 
* @namespace CWN2
* @class Map
* @constructor
* @param {String} div Div id
* @param {Object} options Hashtable of extra options to tag onto the layer
* 
*/

CWN2.Map = OpenLayers.Class(OpenLayers.Map, {
	
    /*
     * Constant: DEFAULT_PARAMS
     * {Object} Hashtable of default parameter key/value pairs 
     */
    DEFAULT_PARAMS: {},
    
    /*
     * Constructor: CWN2.Map
     *
     * Parameters:
     * div - {String} 
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
	
    initialize: function(div, options) {
    	OpenLayers.Map.prototype.initialize.apply(this, arguments);
    },
	
	/**
	* 
	* Load the configuration. 
	* Require that a CWN2.configuration object has been created  
	* 
	* @method loadConfig
	* 
	*/    
    loadConfig: function () {
		
		if (CWN2.base.DEBUG) {
			console.log('CWN2.Map.load');
		}
		
		var config = CWN2.configuration;
		
		// carico le mapOptions
		var mapOptions = config.mapOptions;
		for (var key in mapOptions) {
			this[key] = mapOptions[key];
		}
		
		// aggiungo i layers alla mappa OL
		this.loadLayers(config.layers);

		// registro il controllo per la selezione delle feature dei livelli vettoriali
		this.activateSelectFeatureControl();
    	
		// aggiungo i controlli per la mappa OL
		// TODO
		this.addControl(new OpenLayers.Control.LayerSwitcher({}));


		// vado allo zoom iniziale
		if(!this.getCenter()){
			this.zoomToMaxExtent();
		}

	},

	/**
	* 
	* Add an array of layers to the map. 
	* 
	* @method loadLayers
	* @param [{layerConfig}] layers Array of layerConfig objects 
	* 
	*/    
	
    loadLayers: function (layers) {
		
		if (CWN2.base.DEBUG) {
			console.log('CWN2.Map.loadLayers');
		}

		// aggiungo i layers alla mappa OL
		for (var i = 0; i < layers.length; i++) {
			this.loadLayer(layers[i]);
		}

	},	

	/**
	* 
	* Add a layer to the map. 
	* 
	* @method loadLayer
	* @param {layerConfig} layerConfig A layerConfig object 
	* 
	*/    

    loadLayer: function (layerConfig) {
	
    	if (CWN2.base.DEBUG) {
			console.log('CWN2.Map.loadLayer');
		}

    	// creo il layer OL e lo aggiungo alla mappa
		this.addLayer(CWN2.layerFactory.createLayer(layerConfig));
	},	
	

	/**
	* 
	* Activate the select feature control for all the vector layers of the map. 
	* 
	* @method activateSelectFeatureControl
	* 
	*/ 
	
    activateSelectFeatureControl: function () {
		
		if (CWN2.base.DEBUG) {
			console.log('CWN2.Map.activateSelectFeatureControl');
		}

		// carico l'array con i layer vettoriali
		var vectLayerArray = [];
		for (var i = 0; i < this.layers.length; i++) {
			if (this.layers[i].CLASS_NAME==='OpenLayers.Layer.Vector') {
				this.layers[i].events.on({
				    'featureselected': CWN2.event.onFeatureSelect,
				    'featureunselected': CWN2.event.onFeatureUnselect
				});
				vectLayerArray.push(this.layers[i]);
			}
		}
		// attivo il controllo
		var select_feature_control = new OpenLayers.Control.SelectFeature(vectLayerArray);
		this.addControl(select_feature_control);
		select_feature_control.activate();
		
	},		

	CLASS_NAME: "CWN2.Map"
});


